<link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<style>
    body{
        margin: 0;
        padding: 0;
        font-family: sans-serif;
        background: url(/imgs/bg.jpg)  no-repeat center 0px;
        background-size: cover;
        background-position: center 0;


    }
</style>


<body style="">

    <div class="modal-dialog" style="margin-top: 15%;opacity:0.8;">
        <div class="modal-content">
            <div class="modal-header">
                <h2 class="modal-title text-center" id="myModalLabel">登录</h2>
            </div>
            <div class="modal-body" id = "model-body">
                <div class="form-group">
                    <input type="text" class="form-control" id="username" placeholder="用户名" autocomplete="off">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" id="pass" placeholder="密码" autocomplete="off">
                </div>
            </div>
            <div class="modal-footer" style="text-align: center;">
                <div class="form-group">
                    <a href="/"><button type="button" class="btn btn-info form-control" style="width: 40%;">首页</button></a>
                    <button type="button" id="btn" class="btn btn-success form-control" style="width: 40%;margin-left: 5%;">登录</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    $(function () {
       var user = JSON.parse(sessionStorage.getItem("users"));
       if(user){
           window.location.href = '/';
       }

    });
</script>
<script>
    $('#btn').click(function () {
        var username = $('#username').val();
        var pass = $('#pass').val();
        if(!username){
            swal("登录失败", "用户名不能为空!");
        }else if(!pass){
            swal("登录失败", "密码不能为空!");
        }else{
            $.ajax({
               url:'/loginIn',
               type:'post',
               data:{'username':username,'password':pass},
               success:function (res) {
                   var res = JSON.parse(res);
                   if(res.state == 1){
                       sessionStorage.setItem("users", JSON.stringify(res.data));
                       swal("登录成功！").then( (value)=>{
                           window.location.href = '/';
                       });

                   }else{
                       swal("登录失败！",res.msg);
                   }

               }
            });
        }
    })



</script>